package cn.ly.spring4.aop.xml;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.aspectj.lang.JoinPoint;

public class MethodTest {
	
	private final Map<String,Long> map = new HashMap<String,Long>();
	
	public void beforeMethod(JoinPoint point){
		String objMethod = getMethodName(point);
		long startTime = new Date().getTime();
		map.put(objMethod, startTime);
	}
	
	public void afterMethod(JoinPoint point){
		String objMethod = getMethodName(point);
		long startTime = map.get(objMethod);
		long endTime = new Date().getTime();
		long executeTime = endTime - startTime;
		
		//对执行时间进行处理
		System.out.println("[" + objMethod + "]执行所用时间：" + executeTime + "毫秒");
		
		map.remove(objMethod);
	}
	
	private String getMethodName(JoinPoint point){
		String objMethod = point.getTarget().getClass() + ";method " + point.getSignature().getName() + ";params ";
		Object[] params = point.getArgs();
		for(Object o : params){
			objMethod += o.getClass() + ",";
		}
		objMethod = objMethod.replaceAll("class ", "");
		if(objMethod.endsWith(",")){
			objMethod = objMethod.substring(0, objMethod.lastIndexOf(","));
		}
		return objMethod;
	}
}
